package com.sinodata.bsm.cicp.finder.weblogic9;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class ClusterFinder extends Finder {

    private static final Logger logger = Logger.getLogger(ClusterFinder.class);

    /*@Override
    public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
        List resources = new ArrayList();
        String ip = host;
        String port = attrVals[1].getAttrValue();
        //        MBeanServerConnection connection = JmxUtil.getConnection(
        //                JmxUtil.WEBLOGIC, "192.168.0.211", "7001", "weblogic",
        //        "weblogic1");
        ObjectName service;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(JmxUtil.WEBLOGIC, ip + ":" + port);
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime" + ".DomainRuntimeServiceMBean");
            String attributeNames[] = new String[] { "Name" };
            Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
            ObjectName[] clusters = (ObjectName[]) connection.getAttribute((ObjectName) domainConfiguration, "Clusters");
            for (ObjectName objectName : clusters) {
                Object attrValues[] = null;
                attrValues = JmxUtil.getAttribute(connection, objectName, attributeNames);
                for (int i = 0; i < attrValues.length; i++) {
                    List oneResource = CreateOneResource(attrValues[i].toString(), attributes);
                    resources.add(oneResource);
                }
            }
        } catch (Exception e) {
            logger.error("failed to find weblogic9 Cluster", e);
            resources = null;
        } finally {
            JmxUtil.closeConnector(connector);
        }

        return resources;
    }*/

    /* private List CreateOneResource(String name, ResAttributeValue[] attributes) {
         List aRes = new ArrayList();
         ResValue res = new ResValue();
         ResAttributeValValue valValue = null;
         res.setIp(host); // host IP address
         res.setDescript("cluster " + name);
         res.setInternalId(host + "weblogic9" + name); // Internal ID for agent
         res.setName("cluster." + name);
         aRes.add(res);
         valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), name);
         aRes.add(valValue);
         return aRes;
     }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String port = (String) params.get("PORT");
        ObjectName service;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime" + ".DomainRuntimeServiceMBean");
            String attributeNames[] = new String[] { "Name" };
            Object domainConfiguration = connection.getAttribute(service, "DomainConfiguration");
            ObjectName[] clusters = (ObjectName[]) connection.getAttribute((ObjectName) domainConfiguration, "Clusters");
            for (ObjectName objectName : clusters) {
                Object attrValues[] = null;
                attrValues = JmxUtil.getAttribute(connection, objectName, attributeNames);
                for (int i = 0; i < attrValues.length; i++) {
                    String[] info = new String[4];
                    list.add(info);
                    info[0] = ip + "weblogic9" + attrValues[i].toString();
                    info[1] = "cluster." + attrValues[i].toString();
                    info[2] = "cluster " + attrValues[i].toString();
                    info[3] = attrValues[i].toString();
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find weblogic9 Cluster", e);
            return new ArrayList<String[]>();
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
